package org.kisekiproject.instance {
	import flash.utils.Dictionary;
	
	import org.kisekiproject.evaluator.BindingExpression;
	import org.kisekiproject.evaluator.IVariableNamespace;
	import org.kisekiproject.resources.ResourceLoader;

	/**
	 * Parse XML and create instances.
	 */
	public class InstanceFactory {

		private var _definitions:IDefinitionLibrary;
		private var _resources:ResourceLoader;
		private var _variables:IVariableNamespace;
		private var _createdInstances:Array;
		private var _instanceById:Dictionary;

		/**
		 * Constructor.
		 */
		public function InstanceFactory() {
			_createdInstances=new Array();
			_instanceById=new Dictionary;
		}

		/**
		 * Set library.
		 */
		public function set definitions(value:IDefinitionLibrary):void {
			_definitions=value;
		}

		/**
		 * Set resource manager.
		 */
		public function set resources(value:ResourceLoader):void {
			_resources=value;
		}

		/**
		 * Get resource loader.
		 */
		public function get resources():ResourceLoader {
			return _resources;
		}

		/**
		 * Set library.
		 */
		public function set variables(value:IVariableNamespace):void {
			_variables=value;
		}

		/**
		 * Get variables.
		 */
		public function get variables():IVariableNamespace {
			return _variables;
		}

		/**
		 * Get definitions.
		 */
		public function get definitions():IDefinitionLibrary {
			return _definitions;
		}

		/**
		 * Create instance from xml.
		 */
		public function createInstanceFromXML(source:XML):Instance {
			var instance:Instance=new Instance(source,this);

			_createdInstances.push(instance);

			if (!instance.id)
				instance.id="_"+_createdInstances.length;

			_instanceById[instance.id]=instance;

			return instance;
		}

		/**
		 * Get created instances.
		 */
		public function get createdInstances():Array {
			return _createdInstances;
		}

		/**
		 * Get instance by id.
		 */
		public function getInstanceById(id:String):Instance {
			return _instanceById[id];
		}
	}
}
